package cn.exrick.xboot.modules.withoutInterface;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.constant.SecurityConstant;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.jiuqi.utils.EncryptUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.dao.UserDao;
import cn.exrick.xboot.modules.base.entity.User;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/portalService")
public class QueryRedisToken {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/getToken", method = RequestMethod.POST)
    @ApiOperation(value = "通过身份证号获取Redis中的token")
    public Result<Object> getToken(@RequestBody JSONObject idCard) {
        log.info("验证登陆状态接口，获取接收到的参数" + idCard);
        String idCardS = idCard.getString("idCard");
/*        List<User> user = null;
        try {
            user = userDao.findByIdcardEncryption(EncryptUtil.DESencode(idCardS, CommonConstant.ENCRYPT_KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("在本低查找当前用户是否存在,为零不存在" + user.size());
        if (user.size() == 0) {
            log.info("未找到对应用户，请检查输入的身份证号");
            return new ResultUtil<>().setErrorMsg("未找到对应用户，请检查输入的身份证号");
        }
        String value = redisTemplate.opsForValue().get(SecurityConstant.USER_TOKEN + user.get(0).getUsername());*/
        String value = null;
        try {
            value = redisTemplate.opsForValue().get(SecurityConstant.USER_TOKEN + EncryptUtil.DESdecode(idCardS, CommonConstant.ENCRYPT_KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<Object, String> hashMap = new HashMap<>();
        hashMap.put("token", value);
        hashMap.put("idCard", idCardS);
        if (value == null) {
            log.info("在redis中没有查到当前用户的对应token！");
            return new ResultUtil<Object>().setErrorMsg("该用户未登录，或登陆超时，请重新登录!");
        }
        return new ResultUtil<Object>().setData(hashMap);
    }

}